﻿@inject IHttpService _httpService
@inject NavigationManager NavigationManager
@inject IDataCacheService _dataCacheService

<CnGalWebSite.Components.Containers.MasaTitleContainer Title="随机评价" Icon="fa fa-fw fa-random" Class="mb-4" Outline>

    <ButtonContent>
        <MButton Text Block Color="primary"  OnClick="OnClickMore">
            <MIcon Left>mdi-refresh  </MIcon>
            换一批
        </MButton>
    </ButtonContent>
    <ChildContent>
        <div class="row">
            @foreach (var item in _dataCacheService.RandomUserScoresCatche.Take(_dataCacheService.IsApp?2:6))
            {
                <div class="col-12 col-md-6 col-lg-4 mb-4">
                    <CnGalWebSite.Shared.MasaComponent.PC.Entries.PlayedGames.UserScoreCard Model="item" GameName="@item.GameName"  Clickable OnClick="@(()=>OnClickUser(item))" IsDubbing="item.IsDubbing"/>
                </div>
            }
        </div>
    </ChildContent>

</CnGalWebSite.Components.Containers.MasaTitleContainer>

<CnGalWebSite.Shared.MasaComponent.PC.Entries.PlayedGames.UserScoreDialog @ref="userScoreDialog" />
@code {

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    CnGalWebSite.Shared.MasaComponent.PC.Entries.PlayedGames.UserScoreDialog userScoreDialog;

    protected override async Task OnInitializedAsync()
    {

        await Refresh();
    }


    public async Task Refresh()
    {
        try
        {
            if (_dataCacheService.RandomUserScoresCatche.Count == 0)
            {
                _dataCacheService.RandomUserScoresCatche = await _httpService.GetAsync<List<PlayedGameUserScoreRandomModel>>("api/anniversaries/GetRandomUserScores");
            }
            _dataCacheService.RandomUserScoresCatche.Random();
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取随机用户评分列表失败");
        }
    }

    public void OnClickMore()
    {
        _dataCacheService.RandomUserScoresCatche.Random();
        StateHasChanged();
        
    }


    public void OnClickUser(PlayedGameUserScoreRandomModel model)
    {
        if (userScoreDialog != null)
        {
            userScoreDialog.Refresh(true, model, model.GameName, model.GameId, model.IsDubbing);
        }
    }
}
